class Solution {
public:
    vector<vector<int>> ret;
    vector<int> path;
    bool check[9];
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        dfs(nums);
        return ret;
    }

    void dfs(vector<int>& nums)
    {
        if (path.size() == nums.size())
        {
            ret.push_back(path);
            return;
        }

        for (int i = 0; i < nums.size(); i++)
        {
            if (check[i] == true || (i != 0 && nums[i] == nums[i - 1] && check[i - 1] == false))
                continue;

            path.push_back(nums[i]);
            check[i] = true;

            dfs(nums);

            check[i] = false;
            path.pop_back();
        }
    }
};